// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axiosApi from '@/api/axios.js'
//import store from './vuex/index'

import common from './assets/js/common.js';

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import XLSX from 'xlsx';

import {
	imgBaseURL
} from './api/config.js'

//全局变量绑定
Vue.prototype.imgBaseURL = imgBaseURL;

Vue.use(XLSX);

Vue.use(ElementUI, {
	size: 'small',
	zIndex: 3000
});

Vue.use(common);
Vue.use(axiosApi);

Vue.config.productionTip = false
//Vue.prototype.$http = axios
/* eslint-disable no-new */

// 路由守卫
router.beforeEach((to, from, next) => {
	if(to.matched.some(res => res.meta.isLogin)) { //判断是否需要登录
		if(localStorage['my_token']) {
			next();
		} else {
			let code = getQueryVariable('code');
			if(code) {
				//获取rul参数，调取登录接口
				Vue.prototype.postAxios("/Login/Login?code=" + code, null).then((res) => {
					if(res.code == 0) {
						let token = res.data.token;
						let user = res.data.user;
						user = JSON.stringify(user);
						localStorage.setItem("my_token", token);
						localStorage.setItem("my_user", user);
						next();
					}
				});
			} else {
				yindaoRouter();
			}
		}

	} else {
		next()
	}
});

new Vue({
	el: '#app',
	router,
	//store,
	components: {
		App
	},
	template: '<App/>'
})

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for(var i in vars) {
		var pair = vars[i].split("=");
		if(pair[0] == variable) {
			return pair[1];
		}
	}
	return(false);
}

function yindaoRouter() {
	let eUrl = encodeURI("http://115.26.89.144");
	window.location.href =
		"http://authserver.tmu.edu.cn/authserver/oauth2.0/authorize?response_type=code&client_id=EoCOWF80&redirect_uri=" +
		eUrl +
		"&state=1";
}